package com.zjj.day0425.string;

/**
 * @author zhoujunjie
 * @desc TODO
 * @date 2025/4/25
 */
public class StringBuilderExercises {
    // 练习 1：用 StringBuilder 把数组拼接成一个字符串
    public static String arrayToString(int[] arr) {
        StringBuilder sb = new StringBuilder();
        sb.append("[");
        for (int i = 0; i < arr.length; i++) {
            if (i == arr.length - 1) {
                sb.append(arr[i]);
            } else {
                sb.append(arr[i]).append(", ");
            }
        }
        sb.append("]");
        return sb.toString();
    }

    // 练习 2：利用 StringBuilder 完成字符串反转
    public static String reverseString(String str) {
        StringBuilder sb = new StringBuilder(str);
        return sb.reverse().toString();
    }

    // 练习 3：判断一个字符串是否是对称字符串
    public static boolean isSymmetricString(String str) {
        StringBuilder sb = new StringBuilder(str);
        String reversedStr = sb.reverse().toString();
        return str.equals(reversedStr);
    }

    public static void main(String[] args) {
        int[] arr = {1, 2, 3};
        String result1 = arrayToString(arr);
        System.out.println("练习 1 结果：" + result1);

        String testStr = "hello";
        String result2 = reverseString(testStr);
        System.out.println("练习 2 结果：" + result2);

        String symmetricStr1 = "aba";
        String symmetricStr2 = "abc";
        boolean result3_1 = isSymmetricString(symmetricStr1);
        boolean result3_2 = isSymmetricString(symmetricStr2);
        System.out.println("练习 3 结果 - " + symmetricStr1 + " 是否对称: " + result3_1);
        System.out.println("练习 3 结果 - " + symmetricStr2 + " 是否对称: " + result3_2);
    }
}
